package com.luoxiang.socialandroid.fragment;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;

import static com.luoxiang.socialandroid.common.Constants.MmkvConst.TOKEN;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.lib_common.common.Constants;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.JifenAdapter;
import com.luoxiang.socialandroid.adapter.PointsDetailsAdapter;
import com.luoxiang.socialandroid.base.LazyLoadFragment;
import com.luoxiang.socialandroid.bean.ExchangeLog;
import com.luoxiang.socialandroid.bean.JifentixianBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.WithdrawLog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class WallettixianFragment extends LazyLoadFragment {
    private RecyclerView rv_jifenmingxi;
    private String token;
    private JifenAdapter adapter;
    private SmartRefreshLayout smartRefreshLayout;
    private int page = 1;//是我们的页数
    private ArrayList<WithdrawLog> creditLogs;

    @Override
    protected int setContentView() {
        return R.layout.fragment_pointdetail;
    }

    @Override
    protected void initView() {
        rv_jifenmingxi = findViewById(R.id.rv_jifenmingxi);
        smartRefreshLayout = findViewById(R.id.smartRefresh);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);


    }

    @Override
    protected void lazyLoad() {
        creditLogs = new ArrayList<>();
        getWithdrawLog(EXTRA, token, page, 10);
        adapter = new JifenAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rv_jifenmingxi.setLayoutManager(linearLayoutManager);
        rv_jifenmingxi.setAdapter(adapter);
        smartRefreshLayout.setEnableRefresh(true);
        smartRefreshLayout.setEnableLoadMore(true);
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getWithdrawLog(EXTRA, token, page, 10);
            }
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getWithdrawLog(EXTRA, token, page, 10);
            }
        });
    }

    private void getWithdrawLog(String extra, String token, int page, int limit) {
        getRequest();
        NetUtils.getInstance().getApiService().getWithdrawLog(extra, token, page, limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading();
                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr", decryptStr);
                        Gson gson = new Gson();
                        JifentixianBean jifentixianBean = gson.fromJson(decryptStr, JifentixianBean.class);
                        if (creditLogs != null) {
                            if (page == 1) {
                                creditLogs.clear();
                            }
                        }
                        if (jifentixianBean.getData()!= null && jifentixianBean.getData().getWithdrawLogs() != null && jifentixianBean.getData().getWithdrawLogs().size() > 0) {
                            if (creditLogs != null) {
                                creditLogs.addAll(jifentixianBean.getData().getWithdrawLogs());
                            }
                            adapter.setData(creditLogs);
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.showShort(R.string.mi_wugengduoshuju);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                        if (smartRefreshLayout != null) {
                            smartRefreshLayout.finishRefresh();
                            smartRefreshLayout.finishLoadMore();
                        }
                    }
                });
    }
}
